package net.lahuobao.mall.controller.admin;

import com.google.gson.JsonObject;
import net.lahuobao.mall.entity.admin.MallProductCategorySpecEntity;
import net.lahuobao.mall.entity.admin.MallProductCategorySpecValEntity;
import net.lahuobao.mall.entity.admin.MallProductSpecChildEntity;
import net.lahuobao.mall.service.admin.MallProductCategorySpecServiceI;
import net.lahuobao.mall.service.admin.MallProductCategorySpecValServiceI;
import net.lahuobao.mall.service.admin.MallProductSpecChildServiceI;
import org.apache.log4j.Logger;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**   
 * @Title: Controller
 * @Description: 规格值
 * @author zhangdaihao
 * @date 2016-10-21 21:30:50
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/mallProductCategorySpecValController")
public class MallProductCategorySpecValController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MallProductCategorySpecValController.class);

	@Autowired
	private MallProductCategorySpecValServiceI mallProductCategorySpecValService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	@Autowired
	private MallProductCategorySpecServiceI  specService;

	@Autowired
	private MallProductSpecChildServiceI childService;


	/**
	 * 规格值列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(String specid,HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("net/lahuobao/mall/admin/mallProductCategorySpecValList");
		mv.addObject("specid", specid);
		return mv;
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MallProductCategorySpecValEntity mallProductCategorySpecVal,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MallProductCategorySpecValEntity.class, dataGrid);
		//查询条件组装器
		cq.addOrder("createtime", SortDirection.asc);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallProductCategorySpecVal, request.getParameterMap());
		this.mallProductCategorySpecValService.getDataGridReturn(cq, false);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除规格值 --删除
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(MallProductCategorySpecValEntity mallProductCategorySpecVal, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();

		//判断该规格值是否被引用
		MallProductSpecChildEntity query = new MallProductSpecChildEntity();
		CriteriaQuery cq = new CriteriaQuery(MallProductSpecChildEntity.class);
		query.setCategoryspecvalid(mallProductCategorySpecVal.getId());
		HqlGenerateUtil.installHql(cq, query);
		List<MallProductSpecChildEntity> valLists = childService.getListByCriteriaQuery(cq, false);
		//查询规格值
		mallProductCategorySpecVal = systemService.getEntity(MallProductCategorySpecValEntity.class, mallProductCategorySpecVal.getId());
		if(valLists.size() > 0){
			message = "删除失败,该规格值正在被商品引用不能删除，已将该规格状态改为禁用";
			mallProductCategorySpecVal.setStatus(0);
			mallProductCategorySpecValService.saveOrUpdate(mallProductCategorySpecVal);
		}else{
			message = "规格值删除成功";
			mallProductCategorySpecValService.delete(mallProductCategorySpecVal);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加规格值
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(MallProductCategorySpecValEntity mallProductCategorySpecVal, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(mallProductCategorySpecVal.getId())) {
			message = "规格值更新成功";
			MallProductCategorySpecValEntity t = mallProductCategorySpecValService.get(MallProductCategorySpecValEntity.class, mallProductCategorySpecVal.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(mallProductCategorySpecVal, t);
				mallProductCategorySpecValService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "规格值更新失败";
			}
		} else {
			message = "规格值添加成功";
			mallProductCategorySpecVal.setCreatetime(new Date());
			mallProductCategorySpecVal.setCreateuserid(1);
			mallProductCategorySpecValService.save(mallProductCategorySpecVal);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 规格值列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(MallProductCategorySpecValEntity mallProductCategorySpecVal, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(mallProductCategorySpecVal.getId())) {
			mallProductCategorySpecVal = mallProductCategorySpecValService.getEntity(MallProductCategorySpecValEntity.class, mallProductCategorySpecVal.getId());
		}else{
			//获取规格的数据类型--查询规格
			MallProductCategorySpecEntity  specEntity = specService.get(MallProductCategorySpecEntity.class, mallProductCategorySpecVal.getSpecid());
			mallProductCategorySpecVal.setMallProductCategorySpecEntity(specEntity);
		}
		req.setAttribute("mallProductCategorySpecValPage", mallProductCategorySpecVal);
		return new ModelAndView("net/lahuobao/mall/admin/mallProductCategorySpecVal");
	}

	/**
	 * 根据规格值和所属规格检测规格值唯一性-- 添加
	 */
	@RequestMapping(params = "chkspecval")
	@ResponseBody
	public void checkSpec(HttpServletRequest request, HttpServletResponse response){
		String specId = request.getParameter("specid");
		String value = request.getParameter("param");
		String info = "该规格值可用";
		String status = "y";
		//查询
		MallProductCategorySpecValEntity query = new MallProductCategorySpecValEntity();
		CriteriaQuery cq = new CriteriaQuery(MallProductCategorySpecValEntity.class);
		query.setSpecid(Integer.valueOf(specId));
		query.setValue(value);
		HqlGenerateUtil.installHql(cq, query);
		List<MallProductCategorySpecValEntity> lists = mallProductCategorySpecValService.getListByCriteriaQuery(cq, false);
		if(lists.size() > 0){
			info = "该规格下已经存在该规格值";
			status = "n";
		}
		JsonObject json = new JsonObject();
		json.addProperty("info", info);
		json.addProperty("status", status);
		renderData(response, json.toString());
	}


	/**
	 * 判断是否被引用--编辑前
	 *
	 * @return
	 */
	@RequestMapping(params = "isuses")
	@ResponseBody
	public AjaxJson isUses(MallProductCategorySpecValEntity mallProductCategorySpecVal, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();

		//判断该规格值是否被引用
		MallProductSpecChildEntity query = new MallProductSpecChildEntity();
		CriteriaQuery cq = new CriteriaQuery(MallProductSpecChildEntity.class);
		query.setCategoryspecvalid(mallProductCategorySpecVal.getId());
		HqlGenerateUtil.installHql(cq, query);
		List<MallProductSpecChildEntity> valLists = childService.getListByCriteriaQuery(cq, false);

		if(valLists.size() > 0){
			j.setSuccess(false);
			message = "该规格值正在被引用不能编辑！";
		}else{
			j.setSuccess(true);
		}
		j.setMsg(message);
		return j;
	}
}
